package com.unicom.projt.account;

import com.unicom.projt.Main;
import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.reg.RegisterStepOnePage;
import com.unicom.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-13
 * Time: 下午3:47
 */
public class RetrievePasswordController extends AbstractHttpController<String> {
    private static final String SEND = "SEND";
    private static final String SUBMIT_ONE = "SUBMIT_ONE";
    private static final String SUBMIT_TWO = "SUBMIT_TWO";
    private static final String SUBMIT_THREE = "SUBMIT_THREE";
    private static final String CHECK_NUMBER = "CHECK_NUMBER";
    private static final String GO_TO_NEXT = "GO_TO_NEXT";

    private RetrievePasswordStepOnePage stepOnePage;
    private RetrievePasswordStepTwoPage stepTwoPage;
    private RetrievePasswordStepThreePage stepThreePage;
    private int step = 1;
    private String actionType;

    private String regId;
    private String code;
    private String msgCode;

    public RetrievePasswordController(RetrievePasswordStepOnePage stepOnePage) {
        super(String.class);
        this.stepOnePage = stepOnePage;
        step = 1;
    }

    public RetrievePasswordController(RetrievePasswordStepTwoPage stepTwoPage) {
        super(String.class);
        this.stepTwoPage = stepTwoPage;
        step = 2;
    }

    public RetrievePasswordController(RetrievePasswordStepThreePage stepThreePage) {
        super(String.class);
        this.stepThreePage = stepThreePage;
        step = 3;
    }

    public void checkPhoneNumber(String phoneNumber, String actionType) {
        this.regId = phoneNumber;
        this.actionType = actionType;
        String checkPhoneNumberXml = new PostXmlBuilder().setBiPCode(RequestCode.VERIFY_PHONE_NUMBER).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, checkPhoneNumberXml);
    }

    public void sendVerificationCode(String regId) {
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.SEND_SMS_VERIFY_CODE).setTelNo(regId).createXml();
        this.actionType = SEND;
        this.sendRequest(UriConstants.URL, postXml);


    }

    public void checkAndGoToNextPage(String regId) {
        checkPhoneNumber(regId, GO_TO_NEXT);
    }

    public void submitResetPassword(String regId, String password, String msgCode) {
        this.regId = regId;
        String svcCont = "<SvcCont><TelNo>" + regId + "</TelNo><UserPas>" + password + "</UserPas>" + "<VerificationCode>" + msgCode + "</VerificationCode></SvcCont>";
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.RESET_PASSWORD).setTelNo(regId).setSvcCont(svcCont).createXml();
        this.actionType = SUBMIT_THREE;
        this.sendRequest(UriConstants.URL, postXml);
    }


    @Override
    public void onRequestFinish(String response) {
        if (response == null || "".equals(response)) {
            if (step == 1) {
                stepOnePage.hideProgressDialog();
                ToastUtils.showMessage(stepOnePage, "错误，请稍候再试");
            } else if (step == 2) {
                stepTwoPage.hideProgressDialog();
                ToastUtils.showMessage(stepTwoPage, "错误，请稍候再试");
            } else if (step == 3) {
                stepThreePage.hideProgressDialog();
                ToastUtils.showMessage(stepThreePage, "错误，请稍候再试");
            }
            return;
        }
        String respCode = SimpleXmlParser.getResponseCode(response);
        if (step == 1) {
//            AlertDialogUtils.showConfirmDiaLog(stepOnePage,response);
            stepOnePage.hideProgressDialog();
            if (SEND.equals(actionType)) {
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    ToastUtils.showMessage(stepOnePage, "验证码已经发送.");
                } else {
                    ToastUtils.showMessage(stepOnePage, "验证码发送失败.");
                }
            } else if (CHECK_NUMBER.equals(actionType) || GO_TO_NEXT.equals(actionType)) {
                if (ResponseCode.USER_NOT_EXIST.equals(respCode)) {//未注册用户
                    UserHolder.phoneNumber=regId;
                    ToastUtils.showMessage(stepOnePage, "未注册，请先注册.");
//                    PageJumpUtils.jump(stepOnePage, RegisterStepOnePage.class);
                    stepOnePage.finish();
                } else if (ResponseCode.NOT_UNICOM_NUMBER.equals(respCode)) {//非联通用户
                    ToastUtils.showMessage(stepOnePage, "错误," + SimpleXmlParser.getResponseDes(response));
                } else if ("00102".equals(respCode)) {//注册用户
                    if (CHECK_NUMBER.equals(actionType)) {
                        sendVerificationCode(regId);
                    } else {
                        stepOnePage.gotoNextPage();
                    }
                } else {
                    ToastUtils.showMessage(stepOnePage, "错误," + SimpleXmlParser.getResponseDes(response));
                }
            }
        } else if (step == 2) {
            stepTwoPage.hideProgressDialog();
//            AlertDialogUtils.showConfirmDiaLog(stepTwoPage,response);
            if (SEND.equals(actionType)) {
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    ToastUtils.showMessage(stepTwoPage, "验证码已经发送.");
                } else {
                    ToastUtils.showMessage(stepTwoPage, "验证码发送失败.");
                }
            }
        } else if (step == 3) {
            stepThreePage.hideProgressDialog();
//            AlertDialogUtils.showConfirmDiaLog(stepThreePage,response);
            if (SUBMIT_THREE.equals(actionType)) {
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    UserHolder.phoneNumber=regId;
                    ToastUtils.showMessage(stepThreePage, "重置密码成功.");
                    PageJumpUtils.jump(stepThreePage, Main.class);
                    stepThreePage.finish();
                } else {
                    ToastUtils.showMessage(stepThreePage, "重置密码失败."+SimpleXmlParser.getResponseDes(response));
                }
            }
        }
    }

    @Override
    public void onNetworkAbort() {
        if (step == 1) {
            stepOnePage.hideProgressDialog();
            ToastUtils.showMessage(stepOnePage, "错误，网络连接被中断.");
        } else if (step == 2) {
            stepTwoPage.hideProgressDialog();
            ToastUtils.showMessage(stepTwoPage, "错误，网络连接被中断.");
        } else if (step == 3) {
            stepThreePage.hideProgressDialog();
            ToastUtils.showMessage(stepThreePage, "错误，网络连接被中断.");
        }
    }

    @Override
    public void onNetworkError() {
        if (step == 1) {
            stepOnePage.hideProgressDialog();
            ToastUtils.showMessage(stepOnePage, "错误，网络连接错误.");
        } else if (step == 2) {
            stepTwoPage.hideProgressDialog();
            ToastUtils.showMessage(stepTwoPage, "错误，网络连接错误.");
        } else if (step == 3) {
            stepThreePage.hideProgressDialog();
            ToastUtils.showMessage(stepThreePage, "错误，网络连接错误.");
        }
    }

    @Override
    public void onResponseError() {
        if (step == 1) {
            stepOnePage.hideProgressDialog();
            ToastUtils.showMessage(stepOnePage, "错误，返回结果错误.");
        } else if (step == 2) {
            stepTwoPage.hideProgressDialog();
            ToastUtils.showMessage(stepTwoPage, "错误，返回结果错误.");
        } else if (step == 3) {
            stepThreePage.hideProgressDialog();
            ToastUtils.showMessage(stepThreePage, "错误，返回结果错误.");
        }
    }
}
